package com.fact.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.fact.dto.NodeAttributeDTO;
import com.fact.utils.connection.ConnectionManager;

public class NodeAttributeService {
	
	public ArrayList<NodeAttributeDTO> getAtributeList(int nodeId)
	{
		ArrayList<NodeAttributeDTO> attributeList=new ArrayList<NodeAttributeDTO>();
		Connection conn = ConnectionManager.getConnection();
		   String sql = "Select ns.*,nh.LABEL node_name,MST_MAKES.MAKE_NAME,MST_SCENARIO.SCENARIO_NAME from NODE_SCENARIO_ATTRIBUTES ns ,MST_MAKES,MST_SCENARIO,node_hierarchy nh " +
			"Where ns.MAKE_ID=MST_MAKES.MAKE_ID " +
			"And   ns.SCENARIO_ID=MST_SCENARIO.SCENARIO_ID " +
			"And   ns.SCENARIO_ID=MST_SCENARIO.SCENARIO_ID " +
			"and    nh.NODE_ID=ns.NODE_ID "+
			"And   ns.NODE_ID=? " +
			"ORDER BY ns.make_id,MST_SCENARIO.SCENARIO_ID ";
		   
		   PreparedStatement stmt = null;
		   ResultSet r = null;
			try
			{
				stmt = conn.prepareStatement(sql);
				stmt.setInt(1, nodeId);
				
				r = stmt.executeQuery();
				NodeAttributeDTO na=null;
				while (r.next())
				{
					na=new NodeAttributeDTO();
					na.setMakeId(r.getInt("MAKE_ID"));
					na.setNodeId(r.getInt("NODE_ID"));
					na.setNodeName(r.getString("node_name"));
					na.setScenarioId(r.getInt("SCENARIO_ID"));
					na.setMakeName(r.getString("MAKE_NAME"));
					na.setScenarioName(r.getString("SCENARIO_NAME"));
					na.setMinValue(r.getInt("MIN_VALUE"));
					na.setMaxValue(r.getInt("MAX_VALUE"));
					na.setVisibilityFlag(r.getInt("VISIBILITY_FLAG"));					
					
					attributeList.add(na);
				}
			} 
			catch (Exception e){e.printStackTrace();}
	 		finally{try{stmt.close();ConnectionManager.closeConnection(conn);} catch (Exception e)
				{e.printStackTrace();}stmt = null;conn = null;}
		
		
		return attributeList;
	}
	public boolean updateNodeAttributes(ArrayList<NodeAttributeDTO> attributeList) 
	{
		Connection conn = ConnectionManager.getConnection();
		String sql="";
		int result=0;
		PreparedStatement stmt = null;
			try
			{
				
				for(int i=0;i<attributeList.size();i++)
				{

				sql="Update NODE_SCENARIO_ATTRIBUTES Set Max_Value=?, Min_Value=?, Visibility_Flag=? Where Make_Id=? and Node_Id=? and Scenario_id=?";
				stmt = conn.prepareStatement(sql);
				stmt.setInt(1, attributeList.get(i).getMaxValue());
				stmt.setInt(2, attributeList.get(i).getMinValue());
				stmt.setInt(3, attributeList.get(i).getVisibilityFlag());
				stmt.setInt(4, attributeList.get(i).getMakeId());
				stmt.setInt(5, attributeList.get(i).getNodeId());
				stmt.setInt(6, attributeList.get(i).getScenarioId());
				
				result +=  stmt.executeUpdate();
				}
				
				
			} 
			catch (Exception e){e.printStackTrace();}
	 		finally{try{stmt.close();ConnectionManager.closeConnection(conn);} catch (Exception e)
				{e.printStackTrace();}stmt = null;conn = null;}
	 		
		
	 		if(result==attributeList.size())
				return true;
			else
				return false;
		
		
		
	}
	

}
